<template>
 <div class="rank" ref="rank">
   <scroll class="toplist" ref="toplist" :data="topList">
     <ul>
       <li class="item" v-for="(item,index) in topList" @click="goRoute(item)">
         <div class="icon">
           <img width="100" height="100" v-lazy="item.headPicUrl">
         </div>
         <ul class="songlist">
           <li class="song" v-for="(song,index) in item.song">
            <span>{{song.rank}}</span>
             <span>{{song.title}}-{{song.singerName}}</span>
           </li>
         </ul>
       </li>
     </ul>
   </scroll>
   <router-view></router-view>
   <div class="loading-container" v-show="!topList.length">
     <Loading/>
   </div>
 </div>
</template>

<script>
  import {getRankSongList} from '../../api/rank'
  import {ERR_OK} from '../../api/config'
  import {playlistMixin} from '../../common/js/mixin'
  import Scroll from '../../base/scroll/scroll'
  import Loading from '../../base/loading/loading'
  import {mapMutations} from 'vuex'
  export default {
    name: 'rank',
    mixins:[playlistMixin],
    data(){
      return{
        topList:[],
      }
    },
    components:{
      Scroll,
      Loading
    },
    mounted () {
      this.getRankSongList()
    },
    methods:{
      getRankSongList(){
        getRankSongList().then((res)=>{
          if(ERR_OK === res.code){
            this.normalTopList(res.topList.data.group)
          }
        })
      },
      normalTopList(list){
        list.forEach((item)=>{
          let {toplist} =item
          if(toplist){
            toplist.forEach((item)=>{
              this.topList.push(item)
            })
          }
        })
      },
      ...mapMutations({
        setTopList:'SET_TOP_LIST'
      }),
      handlePlaylist(playlist){
        const bottom = playlist.length ? '60px':''
        this.$refs.rank.style.bottom = bottom
        this.$refs.toplist.refresh();
      },
      goRoute(item){
        this.$router.push({
          path:`/rank/${item.topId}`
        })
        this.setTopList(item)
      }
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .rank
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .toplist
      height: 100%
      overflow: hidden
      .item
        display: flex
        margin: 0 20px
        padding-top: 20px
        height: 100px
        &:last-child
          padding-bottom: 20px
        .icon
          flex: 0 0 100px
          width: 100px
          height: 100px
        .songlist
          flex: 1
          display: flex
          flex-direction: column
          justify-content: center
          padding: 0 20px
          height: 100px
          overflow: hidden
          background: $color-highlight-background
          color: $color-text-d
          font-size: $font-size-small
          .song
            no-wrap()
            line-height: 26px
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
